package com.peak.distribution.jms;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.peak.distribution.entity.order.DisOrder;
import com.peak.distribution.enums.OrderStatus;
import com.peak.distribution.enums.SmsType;
import com.peak.distribution.exception.DistributionError;
import com.peak.distribution.exception.DistributionException;
import com.peak.distribution.service.agent.DisAgentService;
import com.peak.spring.boot.tools.Converts;

/**
 * 发送jms消息的时候发现都要写那么一段内容，于是就有了这个类，把所有的都放到这里来，又不想做成builder模式的类（懒），于是就做成了普通的springbean
 * @author peak
 *
 */
@Service
public class JmsProducerHandler {
	
	@Autowired
	private JmsProducer jmsProducer;
	
	@Autowired
	private Environment env;
	
	@Autowired
	private DisAgentService agentService;

	public void sendLoginJms(HttpServletRequest request, Boolean status) {
		Map<String, String> map = Maps.newHashMap();
		map.put(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY, request.getParameter(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY));
		map.put(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY, request.getParameter(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY));
		map.put("status", status.toString());

		jmsProducer.sendQueue(env.getProperty("mq.queue.login.log", "login.log"), map);
	}
	
	public void sendSmsJms(SmsType type, String mobileNos, String content) {
		Map<String, String> map = Maps.newHashMap();
		map.put("mobileNos", mobileNos);
		map.put("content", content);
		map.put("type", type.name());
		map.put("agentId", Converts.toString(agentService.getCurrentAgentId()));
		String templateCode = null;
		switch (type) {
		case FORGOT_PASSWORD:
		case SUBAGENT_REGIST:
			templateCode = "SMS_125019586";
			break;
		case SHIPPING_ALERT:
			templateCode = "SMS_126464116";
			break;
		default:
			throw new DistributionException(DistributionError.CONFIG_NOT_FOUND.getMessage("阿里云短信模版CODE"));
		}
		map.put("templateCode", templateCode);
		
		jmsProducer.sendQueue(env.getProperty("mq.queue.sms.send", "sms.send"), map);
	}
	
	public void orderChangeStatus(Integer orderId, OrderStatus toStatus) {
		Map<String, String> map = Maps.newHashMap();
		map.put("orderId", Converts.toString(orderId));
		map.put("status", toStatus.name());
		jmsProducer.sendQueue(env.getProperty("mq.queue.order.change.status", "order.change.status"), map);
	}
	
	public void addSubAgentCount(Integer agentId) {
		Map<String, String> map = Maps.newHashMap();
		map.put("agentId", Converts.toString(agentId));
		jmsProducer.sendQueue(env.getProperty("mq.queue.add.sub.agent.count"), map);
	}

	public void addUpSales(DisOrder order) {
		Map<String, String> map = Maps.newHashMap();
		map.put("agentId", Converts.toString(order.getAgent().getId()));
		map.put("orderId", Converts.toString(order.getId()));
		jmsProducer.sendQueue(env.getProperty("mq.queue.order.add.up.sales"), map);
	}
	
	public void addAward(DisOrder order) {
		Map<String, String> map = Maps.newHashMap();
		map.put("agentId", Converts.toString(order.getAgent().getId()));
		map.put("orderId", Converts.toString(order.getId()));
		jmsProducer.sendQueue(env.getProperty("mq.queue.order.add.award"), map);
	}
}
